import { IFeidaoAiWeappPage } from '../../../../interfaces';

const FD_EVENTS_WX_BACKGROUND_AUDIO_CANPLAY = 'fd-events-wx-background-audio-canplay';
const FD_EVENTS_WX_BACKGROUND_AUDIO_PLAY = 'fd-events-wx-background-audio-play';
const FD_EVENTS_WX_BACKGROUND_AUDIO_PAUSE = 'fd-events-wx-background-audio-pause';
const FD_EVENTS_WX_BACKGROUND_AUDIO_STOP = 'fd-events-wx-background-audio-stop';
const FD_EVENTS_WX_BACKGROUND_AUDIO_ENDED = 'fd-events-wx-background-audio-ended';
const FD_EVENTS_WX_BACKGROUND_AUDIO_ONTIMEUPDATA = 'fd-events-wx-background-audio-ontimeupdata';
const FD_EVENTS_WX_BACKGROUND_AUDIO_PREV = 'fd-events-wx-background-audio-prev';
const FD_EVENTS_WX_BACKGROUND_AUDIO_NEXT = 'fd-events-wx-background-audio-next';
const FD_EVENTS_WX_BACKGROUND_AUDIO_ERROR = 'fd-events-wx-background-audio-error';
const FD_EVENTS_WX_BACKGROUND_AUDIO_WAITING = 'fd-events-wx-background-audio-waiting';

/*
 * @see [description](https://mp.weixin.qq.com/debug/wxadoc/dev/api/createInnerAudioContext.html)
 */
export default function create_inner_audio_context(fd: IFeidaoAiWeappPage, src?: string, startTime?: number, title?: string, epname?: string, singer?: string, coverImgUrl?: string, webUrl?: string) {
	if (!fd.data.fd.background_audio_manager) {
		const _ctx = fd.data.fd.background_audio_manager = wx.getBackgroundAudioManager();
		_ctx.onCanplay((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_CANPLAY, errCode);
		});
		_ctx.onPlay((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_PLAY, errCode);
		});
		_ctx.onPause((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_PAUSE, errCode);
		});
		_ctx.onStop((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_STOP, errCode);
		});
		_ctx.onEnded((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_ENDED, errCode);
		});
		_ctx.onTimeUpdate((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_ONTIMEUPDATA, errCode);
		});
		_ctx.onPrev((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_PREV, errCode);
		});
		_ctx.onNext((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_NEXT, errCode);
		});
		_ctx.onError((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_ERROR, errCode);
		});
		_ctx.onWaiting((errCode) => {
			fd.emit(FD_EVENTS_WX_BACKGROUND_AUDIO_WAITING, errCode);
		});
	}
	const ctx = fd.data.fd.background_audio_manager;

	if (src !== undefined) {
		ctx.src = src;
	}
	if (startTime !== undefined) {
		ctx.startTime = startTime;
	}
	if (title !== undefined) {
		ctx.title = title;
	}
	if (epname !== undefined) {
		ctx.epname = epname;
	}
	if (singer !== undefined) {
		ctx.singer = singer;
	}
	if (coverImgUrl !== undefined) {
		ctx.coverImgUrl = coverImgUrl;
	}
	if (webUrl !== undefined) {
		ctx.webUrl = webUrl;
	}

	return ctx;

}
